package com.kelvin.paopao.module.qrcode.webview;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kelvin.paopao.R;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.widgets.CustomToolbar;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/6/19.
 */

public class ScanWebViewActivity extends BaseActivity {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.webView)
    WebView webView;

    private String url;
    private int type;

    public static final int TYPE_CHIFAN = 1000;
    public static final int TYPE_CHANGGE = 1001;
    public static final int TYPE_YUNDONG = 1002;
    public static final int TYPE_QITA = 1003;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_market;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        url = getIntent().getStringExtra("url");
        type = getIntent().getIntExtra("type", 0);

        customToolbar.showBackIcon(this);

        WebSettings mSetting = webView.getSettings();
        mSetting.setJavaScriptEnabled(true);
        mSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);

        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.addJavascriptInterface(this, "parent");
        webView.setWebViewClient(new WebViewClient() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                customToolbar.setTitle(title);
            }
        });

        if (!TextUtils.isEmpty(url)) {
            webView.loadUrl(url);
        } else {
            switch (type) {
                case TYPE_CHANGGE:
                    webView.loadUrl("http://pao.lan361.com/buss/shoplist.aspx?types=changge");
                    break;
                case TYPE_CHIFAN:
                    webView.loadUrl("http://pao.lan361.com/buss/shoplist.aspx?types=chifan");
                    break;
                case TYPE_QITA:
                    webView.loadUrl("http://pao.lan361.com/buss/shoplist.aspx?types=qita");
                    break;
                case TYPE_YUNDONG:
                    webView.loadUrl("http://pao.lan361.com/buss/shoplist.aspx?types=yundong");
                    break;
            }
        }
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    /**
     * js调用此方法. 并且将参数传递过来
     *
     * @param
     */
    @android.webkit.JavascriptInterface
    public void getbuss(final String id, final String name) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent();
                intent.putExtra("id", id);
                intent.putExtra("name", name);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
    }

    public static void launch(Activity activity, int requestCode, int type) {
        Intent intent = new Intent(activity, ScanWebViewActivity.class);
        intent.putExtra("type", type);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void launch(Context context, String url) {
        Intent intent = new Intent(context, ScanWebViewActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

}
